package com.gohome.gohomechat.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Friend;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 16/3/1.
 */
public class SelectorContantListViewAdapter extends BaseAdapter {
    private List<Friend> datas;
    private Context context;
    private boolean[] selectorCon;
    private int checkPosition = 0;

    public SelectorContantListViewAdapter(Context context) {
        this.context = context;
    }

    public String[] getCheck() {
        String[] members = new String[checkPosition];
        for (int i = 0; i < datas.size(); i++) {
            if (selectorCon[i]) {
                members[i] = datas.get(i).getUserName();
            }
        }
        return members;
    }

    public void addData(List<Friend> datas) {
        this.datas = datas;
        if (datas != null && datas.size() > 0) {
            selectorCon = new boolean[datas.size()];
            for (int i = 0; i < datas.size(); i++) {
                selectorCon[i] = false;
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.selectorcontant_listview_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Friend friend = datas.get(position);
        String word = friend.getPinyin().substring(0, 1).toUpperCase();

        holder.position = position;
        // word.toUpperCase();// 字符串小写转大写
        holder.selector_listview_item_word.setText(word);
        holder.selector_listview_item_username.setText(friend.getUserName());
        Picasso.with(context).load(friend.getHead()).into(holder.selector_listview_item_head);
        //下标为0的显示
        if (position == 0) {
            holder.selector_listview_item_word.setVisibility(View.VISIBLE);
        } else {
            //取出上一个friend, 并得到的第一个word
            String preWord = datas.get(position - 1).getPinyin().substring(0, 1).toUpperCase();
            ;
            // preWord.toUpperCase();
            //判断是否于当前行的word是否相同
            //如果相同, 隐藏
            if (word.equals(preWord)) {
                holder.selector_listview_item_word.setVisibility(View.GONE);
            } else {
                //如果不同, 显示
                holder.selector_listview_item_word.setVisibility(View.VISIBLE);
            }
        }
        holder.selector_listview_item_selector.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkPosition++;
                    selectorCon[holder.position] = true;
                } else {
                    checkPosition--;
                    selectorCon[holder.position] = false;
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        private int position;
        private CheckBox selector_listview_item_selector;
        private TextView selector_listview_item_word, selector_listview_item_username;
        private ImageView selector_listview_item_head;

        public ViewHolder(View view) {
            selector_listview_item_selector = (CheckBox) view.findViewById(R.id.selector_listview_item_selector);
            selector_listview_item_word = (TextView) view.findViewById(R.id.selector_listview_item_word);
            selector_listview_item_username = (TextView) view.findViewById(R.id.selector_listview_item_username);
            selector_listview_item_head = (ImageView) view.findViewById(R.id.selector_listview_item_head);
        }
    }
}
